﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotellApplikationASP.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

namespace HotellApplikationASP
{
    public partial class About : Page
    {
        private HotellbokningASPEntities db;
        private MailClient mc;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new HotellbokningASPEntities();
            mc = new MailClient();
        }

        protected void ButtonPaging_Click(object sender, EventArgs e)
        {
            GridView2.AllowPaging = !GridView2.AllowPaging;
        }

        public IQueryable<Order> GridView2_GetData()
        {
            var ID = User.Identity.GetUserId();
            return db.Orders.Where(r => r.CustomId == ID);
        }

        //protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    GridViewRow row = GridView2.SelectedRow;
        //    string ID = row.Cells[1].Text;

        //    var ds = db.RoomOrderDetails.Where(r => r.OrderId == int.Parse(ID));
      
        //}

        protected void GridView2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            btnAnnulate.Enabled = false;
            GridViewRow row = GridView2.SelectedRow;
            int ID = int.Parse(row.Cells[1].Text);
            Session["tempID"] = ID;

            var ds = db.RoomOrderDetails.Where(r => r.OrderId == ID).ToList();
            Repeater1.DataSource = ds;
            Repeater1.DataBind();

            foreach (RoomOrderDetail room in ds)
            {
                if (room.StartDate > DateTime.Today.AddDays(10))
                {
                    btnAnnulate.Enabled = true;
                    break;
                }
            }
            

        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var RoomOrder = (RoomOrderDetail)e.Item.DataItem;
            ((Literal)e.Item.FindControl("litRoomID")).Text = RoomOrder.RoomId.ToString();
            ((Literal)e.Item.FindControl("litStartDate")).Text = RoomOrder.StartDate.ToString();
            ((Literal)e.Item.FindControl("litEndDate")).Text = RoomOrder.EndDate.ToString();
            ((Literal)e.Item.FindControl("litTotal")).Text = RoomOrder.Total.ToString();

        }

        protected void btnAnnulate_Click(object sender, EventArgs e)
        {
            var ord = db.Orders.Find(Session["tempID"]);
            db.Orders.Remove(ord);
            db.SaveChanges();

            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var user = manager.FindById(User.Identity.GetUserId());

            var thread = new Thread(delegate()
            {
                mc.MyMail(user.Email, "Annulerad bokning", "Du har annulerat bokning no. " + ord.Id.ToString());
            });
            thread.Start();
            

            Response.Redirect(Request.RawUrl);
        }

    }
}